/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package patterns;

import javax.swing.*;
import java.awt.*;
import java.util.concurrent.atomic.AtomicInteger;

public class CompositeTest {

    public static void main(String[] args)
            throws InterruptedException {
        JFrame frame = new JFrame();
        frame.setSize(450, 450);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container pane = frame.getContentPane();
        pane.setLayout(null);

        // component #0
        Component component0 = new JButton("Button #0");
        pane.add(component0);
        component0.setBounds(10, 10, 90, 90);

        // component #1
        Component component1 = new JButton("Button #1");
        pane.add(component1);
        component1.setBounds(110, 110, 90, 90);

        // component #2
        final Container component2 = new Container();
        pane.add(component2);
        component2.setBounds(200, 200, 200, 200);
        {
            // sub-component #2-0
            Component component2_0 = new JButton("Button #2-0");
            component2.add(component2_0);
            component2_0.setBounds(10, 10, 90, 90);
            // sub-component #2-0
            Component component2_1 = new JButton("Button #2-1");
            component2.add(component2_1);
            component2_1.setBounds(110, 110, 90, 90);
        }

        frame.setVisible(true);

        final AtomicInteger angle = new AtomicInteger(0);
        while (true) {
            Thread.sleep(40);
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    int a = angle.incrementAndGet();
                    component2.setBounds(
                            200 + (int) (20 * Math.cos(a / 50.0)),
                            200 + (int) (20 * Math.sin(a / 50.0)),
                            200, 200);
                }
            });
        }
    }
}